class MovieClipManager {
    private pngObj: Object = {}; // 图片文件对象池
    private jsonObj: Object = {}; // json文件对象池
    private roleObj: Object = {}; // movieClip对象池

    public clear(imgName?: string, jsonName?: string, actionName?: string) {
        if (!imgName) {
            this.pngObj = {};
        } else {
            if (this.pngObj[imgName]) this.pngObj[imgName] = null;
        }
        if (!jsonName) {
            this.jsonObj = {};
        } else {
            if (this.jsonObj[jsonName]) this.jsonObj[jsonName] = null;
        }
        if (!actionName) {
            this.roleObj = {};
        } else {
            var role = this.roleObj[actionName] ? this.roleObj[actionName] : null;
            if (role) {
                if (role.parent) role.parent.removeChild(role);
                role = null;
            }
        }
    }

    /**
     * pngName: 图片名
     * jsonName: json配置项名称
     * actionName: action名
     * callback: 获取对象后的回调
     */
    public async play(pngName: string, jsonName: string, actionName: string) {
            // 先检查资源是否存在 如果存在 就直接在object中取
            if (this.checkArr(pngName, this.pngObj) && this.checkArr(jsonName, this.jsonObj)) {
                // movieClip对象池中是否存在对应的action 有 就直接返回 没有 就创建
                if (this.checkArr(actionName, this.roleObj)) {
                    return this.roleObj[actionName];
                };
                var mcData = this.jsonObj[jsonName];
                var mcTexture = this.pngObj[pngName];
                var mcDataFactory = new egret.MovieClipDataFactory(mcData, mcTexture);
                var role: egret.MovieClip = new egret.MovieClip(mcDataFactory.generateMovieClipData(actionName));
                return role;
            } else {
                // 资源不存在 那么就获取资源
                var role1 = await this.loadSource(pngName, jsonName, actionName);
                return role1;
            }
        
    }

    private async loadSource(pngName: string, jsonName: string, actionName: string) {
        var self = this;
        return new Promise((reslove,reject)=>{
            var mcTexture, mcData;
            var check = function () {
                if (mcTexture && mcData) {
                    // 两个资源齐了 放进对象池 并创建对象
                    self.jsonObj[jsonName] = mcData;
                    self.pngObj[pngName] = mcTexture;
                    var mcDataFactory = new egret.MovieClipDataFactory(mcData, mcTexture);
                    var role: egret.MovieClip = new egret.MovieClip(mcDataFactory.generateMovieClipData(actionName));
                    self.roleObj[actionName] = role;
                    reslove(role);
                }
            }
            if (this.checkArr(pngName, this.pngObj)) {
                mcTexture = self.pngObj[pngName];
            } else {
                // var loader = new egret.URLLoader();
                // loader.addEventListener(egret.Event.COMPLETE, function loadOver(e) {
                //     var loader = e.currentTarget;
                //     mcTexture = loader.data;
                //     check();
                // }, this);
                // loader.dataFormat = egret.URLLoaderDataFormat.TEXTURE;
                // var request = new egret.URLRequest(config.staticurl + "/resource/assets/mc/" + pngName + '.png');
                // loader.load(request);
                mcTexture = RES.getRes(pngName+'_png');
                check();
            }

            if (this.checkArr(jsonName, this.jsonObj)) {
                mcData = self.jsonObj[jsonName];
            } else {
                // var loader = new egret.URLLoader();
                // loader.addEventListener(egret.Event.COMPLETE, function loadOver(e) {
                //     var loader = e.currentTarget;
                //     mcData = JSON.parse(loader.data);
                //     check();
                // }, this);
                // loader.dataFormat = egret.URLLoaderDataFormat.TEXT;
                // var request = new egret.URLRequest(config.staticurl + "/resource/assets/mc/" + jsonName + '.json');
                // loader.load(request);
                mcData = RES.getRes(jsonName+'_json');
                check();
            }
        });
        
    }

    // 检查数组中是否存在某个key
    private checkArr(name: string, obj: Object): boolean {
        // 检测两个对象池
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                var element = obj[key];
                if (key == name && element != undefined && element != null) return true;
            }
        }
        return false;
    }

}